#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
# @Time    : 2021/2/24 下午8:20
# @Author  : HuiDong.Bai
# @Email   : huidong.bai@ximalaya.com
# @File    : download_log.py
# @Software: PyCharm
import os
import sys

import requests
from lxml import etree
from concurrent.futures import ThreadPoolExecutor, as_completed

# 保存日志连接
log_list = []


# 获取响应体token和seesion
def get_token_session(rsp):
    msg = rsp.headers.get('Set-Cookie')
    x_token = msg.split('; expires=')[0].split('=')[1]
    l_session = msg.split('; expires=')[1].split('laravel_session=')[1]
    return x_token, l_session


# 获取_token值
def get_token(rsp):
    html = etree.HTML(rsp.text)
    text = html.xpath("//*[@name='_token']/@value")
    return text[0]


def get_login_info():
    login_url = 'https://admin-skill-pub.xiaoyastar.work/admin/auth/login'
    headers = {'Host': 'admin-skill-pub.xiaoyastar.work'}
    resp = requests.get(url=login_url, headers=headers)

    temporary_token, temporary_session = get_token_session(resp)
    _token = get_token(resp)

    re_headers = {'Host': 'admin-skill-pub.xiaoyastar.work',
                  'Cookie': f'XSRF-TOKEN={temporary_token}; laravel_session={temporary_session}'}

    # 用户名：baihuidong, 密码：123456
    data = {'username': 'baihuidong', 'password': '123456', '_token': _token}

    re_resp = requests.post(url=login_url, headers=re_headers, data=data)
    xsrf_token, laravel_session = get_token_session(re_resp)
    return xsrf_token, laravel_session


def get_log_list(next_url, header):
    if len(next_url):
        print('这是测试log')
        new_url = next_url[0]
        rep = requests.get(url=new_url, headers=header)
        html = etree.HTML(rep.text)
        log_links = html.xpath("//tr/td/a/@href")
        if len(log_links):
            log_list.extend(log_links)
        next2_url = html.xpath("//*[@id='example2_next']/a/@href")
        return get_log_list(next2_url, header)
    else:
        print('这是结束log')
        return 0


def download(log_path, link):
    file_name = log_path + '/' + (sn + link.split(sn)[1]).replace('/', '-')
    print('Downloading the %s' % file_name)
    down_file = requests.get(url=link, headers=headers)

    with open(file_name, "wb") as f:
        f.write(down_file.content)


if __name__ == '__main__':
    xsrf_token, laravel_session = get_login_info()

    # 获取日志重定向位置，在桌面下创建logs文件夹，日志将存在该文件夹下
    desktop_path = os.path.join(os.path.expanduser("~"), "Desktop")
    log_path = desktop_path + '/download_logs'
    if not os.path.exists(log_path):
        os.makedirs(log_path)

    sn = '1110000102130196015738'
    date = '20210330'

    # sn = sys.argv[1]
    # date = sys.argv[2]

    url = f'https://admin-skill-pub.xiaoyastar.work/admin/qCloud/logList?newDir=S_PROD1_MEIDIMINI&sn={sn}&date={date}'
    headers = {'Host': 'admin-skill-pub.xiaoyastar.work',
               'Cookie': f'XSRF-TOKEN={xsrf_token}; laravel_session={laravel_session}'}

    re = requests.get(url=url, headers=headers)
    html = etree.HTML(re.text)
    log_links = html.xpath("//tr/td/a/@href")
    example2_next = html.xpath("//*[@id='example2_next']/a/@href")

    if len(log_links) == 0:
        print('没找到相关日志')
        sys.exit()

    log_list.extend(log_links)
    get_log_list(example2_next, headers)
    log_list = list(set(log_list))

    with ThreadPoolExecutor(max_workers=len(log_list)) as t:
        obj_list = []
        for link in log_list:
            obj = t.submit(download, log_path, link)
            obj_list.append(obj)
